/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/

#ifndef __CC_PYTHON_IMPORT__
#define __CC_PYTHON_IMPORT__

#include <python_import_macro.h>
#include <python_import_impl.h>
#include <cocos2d.h>
#include <extensions/singleton.hpp>
#include <extensions/io.hpp>

NS_CCPY_BEGIN

	class CPythonImport
	{
		DECLARE_SINGLETON(CPythonImport)
	public:
		void Initialize()
		{
			std::string strPath = cocos2d::CCFileUtils::sharedFileUtils()->getWritablePath();
			std::string strPPKPath = strPath + "bin/";
			std::string strGamePPKPath = strPPKPath + "game.ppk";
			std::string strSystemPPKPath = strPPKPath + "system.ppk";


			if(!CIOUtils::GetInstance()->DirectoryExists(strPPKPath))
				CIOUtils::GetInstance()->MakeDirectory(strPPKPath);

			if(!CIOUtils::GetInstance()->FileExists(strGamePPKPath))
				CIOUtils::GetInstance()->CopyAssertFileToCwd("bin/game.ppk", strGamePPKPath.c_str());
			if(!CIOUtils::GetInstance()->FileExists(strSystemPPKPath))
				CIOUtils::GetInstance()->CopyAssertFileToCwd("bin/system.ppk", strSystemPPKPath.c_str());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
			Py_Initialize();
#else
			cocos2d::CCLog(strSystemPPKPath.c_str());
			Py_Initialize(strSystemPPKPath.c_str());
#endif
			CPythonImportImpl::GetInstance()->RegisterObjects();
			CPythonImportImpl::GetInstance()->RegisterDenshionObjects();
			CPythonImportImpl::GetInstance()->RegisterExtensionObjects();
			PyErr_Clear();			
			char szScriptPath[2048];
			char szAppPath[2048];
			strcpy(szAppPath, strPath.c_str());
			szAppPath[strlen(szAppPath) - 1] = 0;
			sprintf(szScriptPath, "import sys;sys.path.append(r'%s');sys.path.append(r'%s');print sys.path", szAppPath, strGamePPKPath.c_str());
			PyRun_SimpleString(szScriptPath);
			m_pyGameModule = NULL;
		}

		void Destroy()
		{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#endif
			cocos2d::CCLog("Py_Finalize");
			Py_Finalize();
		}

		bool load(const char *module_name)
		{			
			PyErr_Clear();
			m_pyGameModule = PyImport_ImportModule(module_name);
			if(!m_pyGameModule && PyErr_Occurred())
			{
				PyErr_Print();
				return false;
			}
			PyObject *pyModDict = PyModule_GetDict(m_pyGameModule);
			if(!pyModDict && PyErr_Occurred())
			{
				PyErr_Print();
				return false;
			}
			PyObject *pyMain = PyDict_GetItemString(pyModDict, "__GameMain__");
			if(!pyMain && (!PyCallable_Check(pyMain) || PyErr_Occurred()))
			{
				PyErr_Print();
				return false;
			}
			PyObject_CallFunction(pyMain, NULL);
			if(PyErr_Occurred())
			{
				PyErr_Print();
				return false;
			}
			
			return true;
		}
	private:
		PyObject *m_pyGameModule;
	};

NS_CCPY_END

#endif